//
//  WZRefreshView.swift
//  weibo
//
//  Created by mac on 16/3/4.
//  Copyright © 2016年 mac. All rights reserved.
//

import UIKit
import SnapKit
class WZRefreshView: UIControl {
    
    enum refreshType: Int{
        
        case  Normal = 0
        case Pulling = 1
        case Loading = 2
        
        
    }
    var scrollView: UIScrollView?
    //    MARK:这个属性有什么用
    var oldRefreshStatu: refreshType?
    var refreshStatu: refreshType = .Normal {
        didSet{
            
            switch refreshStatu{
                
            case .Normal: messageLable.text = "下拉刷新"
          
                print("下拉刷新")
            case.Pulling: messageLable.text = "释放刷新"
         
                  print("释放刷新")
            case.Loading: messageLable.text = "正在刷新"
         
                  print("正在刷新")
            
//            加载的时候让refresh显示在nacBar下面
           var inset = scrollView?.contentInset
            inset?.top = 64
        scrollView?.contentInset = inset!
            
                sendActionsForControlEvents(UIControlEvents.ValueChanged)
            // 记录加载的状态
            oldRefreshStatu = .Loading
                //需要恢复回去

            }
            
            
        }
    }
    
    
    func endRefresh(isSuccess: Bool){
        if isSuccess{
            //        将偏移量设置回去
            var inset = scrollView?.contentInset
            inset?.top = 64
            scrollView?.contentInset = inset!
            refreshStatu = .Normal
        }else{
            
            //        将偏移量设置回去
            var inset = scrollView?.contentInset
            inset?.top = 50
            scrollView?.contentInset = inset!
            sleep(2)
            refreshStatu = .Normal
            
        }
//        将偏移量设置回去
        var inset = scrollView?.contentInset
        inset?.top = 64
        scrollView?.contentInset = inset!
        refreshStatu = .Normal
//         加载完成
//        refreshStatu = .Normal
//        
//        if oldRefreshStatu == .Loading{
//            
//            //加载的时候,让视图显示在 Nav下边 修改  scrollView?.contentInset
//            var inset = scrollView?.contentInset
//            inset?.top -= 64
//            scrollView?.contentInset = inset!
//            
//            //恢复标记
//            oldRefreshStatu = .Normal
//        
//        }
//
    }
    
    
    override func willMoveToSuperview(newSuperview: UIView?) {
        let scrollView = newSuperview as? UIScrollView
        self.scrollView = scrollView
        scrollView?.addObserver(self, forKeyPath: "contentOffset", options: NSKeyValueObservingOptions.New, context: nil)
    }
    
    override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) {
        
        let scrollView = object as! UIScrollView
        //        print(scrollView.contentOffset)
        
        if scrollView.dragging{//正在下拉
            
            if scrollView.contentOffset.y <= -150{
                refreshStatu = .Pulling
                 print(refreshStatu)
            }else if scrollView.contentOffset.y >= -150 && scrollView.contentOffset.y <= -114{
                refreshStatu = .Normal
                 print(refreshStatu)
            }
            
        }else{//手已经释放了
            if scrollView.contentOffset.y <= -150 && refreshStatu == .Pulling {
                                print(refreshStatu)
                refreshStatu = .Loading
            }
        }
        
        
        
    }
    
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = UIColor.yellowColor()
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    

    
    
   //    MARK:将3个控件添加到父控件，并设置约束,并且设置refreshView的frame
    private func setupUI(){
        
        var refreshFram = frame
        refreshFram.size = CGSizeMake(UIScreen.mainScreen().bounds.width, 50)
        refreshFram.origin.y = -50
        frame = refreshFram
        
        
        addSubview(arrowImagView)
        addSubview(messageLable)
        addSubview(loadingImageView)
        
        messageLable.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self.snp_centerX).offset(15)
            make.centerY.equalTo(self.snp_centerY)
        }
        arrowImagView.snp_makeConstraints { (make) -> Void in
            make.centerY.equalTo(self.snp_centerY)
            make.right.equalTo(messageLable.snp_left).offset(5)
        }
        loadingImageView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(arrowImagView.snp_center)
        }
        
    }
    
    //    MARK:懒加载下拉arrowImagView,messageLable,loadingImageView
    lazy var arrowImagView: UIImageView = {
    let imageView = UIImageView()
        imageView.image = UIImage(named: "tableview_pull_refresh")
        return imageView
    }()
    lazy var loadingImageView: UIImageView = {
        let imageView = UIImageView()
        imageView.image = UIImage(named: "tableview_loading")
        return imageView
    }()
    lazy var messageLable: UILabel = {
    let lable = UILabel()
        lable.text = "下拉刷新"
        lable.textAlignment = .Center
        lable.textColor = UIColor.blackColor()
        lable.font = UIFont.systemFontOfSize(13)
        return lable
    }()
}
